package com.example.a10928.weifriend.ui.home.menu

import android.content.Intent
import android.support.v7.app.AppCompatActivity
import android.os.Bundle
import android.support.v7.widget.LinearLayoutManager
import android.text.InputType
import android.text.TextUtils
import android.view.View
import android.widget.EditText
import com.example.a10928.weifriend.App
import com.example.a10928.weifriend.R
import com.example.a10928.weifriend.adapter.ActiveDetailILoader
import com.example.a10928.weifriend.adapter.ActiveOrderAdapter
import com.example.a10928.weifriend.http.api.RowsWrapper
import com.example.a10928.weifriend.http.api.VariableName
import com.example.a10928.weifriend.http.api.base.ActiveDetailBean
import com.example.a10928.weifriend.http.api.base.ActivityShopBean
import com.example.a10928.weifriend.http.api.base.MyAdertBean
import com.example.a10928.weifriend.util.RxUtil
import com.mylhyl.circledialog.CircleDialog
import com.mylhyl.circledialog.callback.ConfigInput
import com.mylhyl.circledialog.view.listener.OnInputClickListener
import io.reactivex.functions.Consumer
import kotlinx.android.synthetic.main.activity_active_detail.*

class ActiveDetailActivity : AppCompatActivity() {

    var imgList:MutableList<ActiveDetailBean.ImagesBean> = mutableListOf()

    var type="1"

    var adapter:ActiveOrderAdapter ?=null

    var data:MutableList<ActiveDetailBean.ShopOrderBean> = mutableListOf()

    override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)
        setContentView(R.layout.activity_active_detail)

        type = intent.getStringExtra(VariableName.TYPE)
        var   id = intent.getIntExtra(VariableName.DATA,0)

     //   imgList=intent.getParcelableArrayListExtra<ActivityShopBean.DataBean.ImagesBean>(VariableName.IMG).toMutableList();
        when(type){
            "1"->{
                tv_parti.visibility= View.VISIBLE

                tv_parti.setOnClickListener {

                    var dialog=CircleDialog.Builder()
                            .setCanceledOnTouchOutside(false)
                            .setCancelable(true)
                            .setInputManualClose(true)
                            .setTitle("份数")
                            .setSubTitle("请输入买入份数")
                            .configInput(ConfigInput{params ->
                                params.inputType= InputType.TYPE_CLASS_NUMBER
                            })
                            .autoInputShowKeyboard()
                            .setNegative("取消",null)
                            .setPositiveInput("确定",object : OnInputClickListener {
                                override fun onClick(text: String?, v: View?) {
                                    if(TextUtils.isEmpty(text)){
                                        return
                                    }
                                    var intent : Intent = Intent(this@ActiveDetailActivity,PaySelectActvitiy::class.java)
                                    intent.putExtra(PaySelectActvitiy.SOURCE_PAYSELECTACTIVITY_TYPE,PaySelectActvitiy.SOURCE_TAKE_ACTIVITY);
                                    intent.putExtra(VariableName.DATA,id)
                                    intent.putExtra(VariableName.TWOTYPE,text)
                                    startActivity(intent)
                                }

                            })
                            .show(supportFragmentManager)



                }
                App.app().bpService.get_shop_detail(id.toString())
                        .compose(RxUtil.io2mainLoad(this))
                        .subscribe(
                                Consumer {

                                    initView(it)

                                },
                                Consumer {
                                    RxUtil.errorException(it)
                                }
                        )
            }
            "3"->{

                ll_order.visibility=View.VISIBLE
/*                data.add(MyAdertBean.RecordsBean())
                data.add(MyAdertBean.RecordsBean())
                data.add(MyAdertBean.RecordsBean())*/
                adapter=ActiveOrderAdapter(R.layout.item_active_order,data)
                var layout=LinearLayoutManager(this)
                recyc.layoutManager=layout
                recyc.adapter=adapter

                App.app().bpService.get_my_detail(App.app().user.userId,App.app().user.token,id.toString())
                        .compose(RxUtil.io2mainLoad(this))
                        .subscribe(
                                Consumer {
                                    initView(it)
                                    data.clear()

                                    data.addAll(it.data.shop_order)
                                    adapter?.notifyDataSetChanged()

                                },
                                Consumer {
                                    RxUtil.errorException(it)
                                }
                        )
            }
            "2"->{
                ll_xy.visibility=View.VISIBLE

                App.app().bpService.get_shop_detail(id.toString())
                        .compose(RxUtil.io2mainLoad(this))
                        .subscribe(
                                Consumer {

                                    tv_id.text= "ID: "+it.data.user_info.user_name
                                    tv_number.text="中奖编号: "+it.data.win_code

                                    initView(it)

                                },
                                Consumer {
                                    RxUtil.errorException(it)
                                }
                        )
            }
        }



        iv_back.setOnClickListener {
            finish()
        }





    }

    fun initView(it:RowsWrapper<ActiveDetailBean>){
        tv_title.text=it.data.title
        tv_total.text="总份额："+it.data.need_people
        tv_plus.text="剩余份额："+(it.data.need_people-it.data.join_people)
        tv_product.text=it.data.content
        //  tv_num.text="期数编号:"+it.data.win_code
        pb.progress=(100* it.data.join_people.toDouble()/it.data.need_people.toDouble()).toInt()

        imgList=it.data.images

        banner.setImageLoader(ActiveDetailILoader())
        banner.setImages(imgList)
        banner.start()
    }

}
